﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Croxweb.Leuphana.Controls
{
    public partial class MyStudyLogin : UserControl
    {
        public MyStudyLogin()
        {
            InitializeComponent();

            // Set default values to dependency properties.
            OnLoggedInChanged(false, (Boolean)GetValue(IsLoggedInProperty));
        }

        #region IsLoggedIn DependencyProperty

        public Boolean IsLoggedIn
        {
            get { return (Boolean)GetValue(IsLoggedInProperty); }
            set { SetValue(IsLoggedInProperty, value); }
        }

        public static readonly DependencyProperty IsLoggedInProperty =
            DependencyProperty.Register("IsLoggedIn", typeof(Boolean), typeof(MyStudyLogin), new PropertyMetadata(true, OnLoggedInChanged));

        private static void OnLoggedInChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ((MyStudyLogin)o).OnLoggedInChanged((Boolean)e.OldValue, (Boolean)e.NewValue);
        }

        private void OnLoggedInChanged(Boolean oldValue, Boolean newValue)
        {
            if (newValue != oldValue)
            {
                if (newValue)
                {
                    Visibility = Visibility.Collapsed;
                }
                else
                {
                    Visibility = Visibility.Visible;
                }
            }
        }

        #endregion

        #region Text Input Events

        private void password_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                NotifyLogin();
            }
        }

        private void username_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                password.Focus();
            }
        }

        #endregion

        #region Button Events

        private void Login_Click(object sender, RoutedEventArgs e)
        {
            NotifyLogin();
        }

        private void Portal_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler handler = ShowPortalClick;
            if (null != handler)
            {
                handler(this, e);
            }
        }

        private void Cert_Click(object sender, RoutedEventArgs e)
        {
            RoutedEventHandler handler = ShowCertClick;
            if (null != handler)
            {
                handler(this, e);
            }
        }       
        
        #endregion

        public event RoutedEventHandler ShowPortalClick;

        public event RoutedEventHandler ShowCertClick;

        public event EventHandler<LoginEventArgs> Login;

        private void NotifyLogin()
        {
            EventHandler<LoginEventArgs> handler = Login;
            if (null != handler)
            {
                handler(this, new LoginEventArgs(username.Text, password.Password));
            }
        }


    }

    public class LoginEventArgs : EventArgs
    {
        public LoginEventArgs(String username, String password) : base()
        {
            this.Username = username;
            this.Password = password;
        }

        public String Username;
        public String Password;
    }
}
